package com.miku.blog.runner;

import com.miku.blog.domain.UniqueView;
import com.miku.blog.domain.pojo.SystemConstance;
import com.miku.blog.service.ArticleService;
import com.miku.blog.service.UniqueViewService;
import com.miku.blog.utils.DateUtils;
import com.miku.blog.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author shkstart
 * @create ${}YEAR-06-12 23:35
 */
@EnableScheduling
@Configuration
public class RedisScheduling {

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private UniqueViewService uniqueViewService;
    @Autowired
    private ArticleService articleService;

    //定时更新访问量
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void redisUniqueViewScheduled() {

        Date todayMorning = DateUtils.getDateByTodayMorning();
        SimpleDateFormat simpleDateFormat = DateUtils.getSimpleDateFormatYMDHMS();
        String format = simpleDateFormat.format(todayMorning);

        if (redisCache.hasHashKey(SystemConstance.REDIS_UNIQUE_VIEW_KEY, format)) {

            updateUniqueView(format, todayMorning);

        } else {
            redisCache.setCacheHashMap(SystemConstance.REDIS_UNIQUE_VIEW_KEY,
                    format,
                    SystemConstance.UNIQUE_VIEW_INIT);

            Date yesterday = DateUtils.getDateByYesterday();
            String yesterdayFormat = simpleDateFormat.format(yesterday);
            if (redisCache.hasHashKey(SystemConstance.REDIS_UNIQUE_VIEW_KEY, yesterdayFormat)) {
                updateUniqueView(yesterdayFormat, yesterday);
                redisCache.delCacheHashMaps(SystemConstance.REDIS_UNIQUE_VIEW_KEY,yesterdayFormat);
            }
        }

        setRedisExpireTime(SystemConstance.REDIS_UNIQUE_VIEW_KEY);

    }


    private void updateUniqueView(String key, Date date) {
        Integer viewCount = redisCache.getCacheHashMapValue(SystemConstance.REDIS_UNIQUE_VIEW_KEY, key);
        UniqueView uniqueView = uniqueViewService.getUniqueViewByCreateTime(date);

        if (Objects.nonNull(uniqueView)) {
            Integer count = uniqueView.getViewsCount();
            if (viewCount > count) {
                uniqueView.setViewsCount(viewCount);
                uniqueViewService.updateById(uniqueView);
            }
        } else {
            UniqueView uniqueViewNew = new UniqueView();
            uniqueViewNew.setViewsCount(viewCount);
            uniqueViewNew.setCreateTime(date);
            uniqueViewService.save(uniqueViewNew);
        }
    }

    //定时更新 阅读量
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void redisArticleViewCountScheduled() {

        Map<String, Integer> viewCountMap = redisCache.getCacheHash(SystemConstance.REDIS_ARTICLE_VIEW_COUNT_KEY);
        Set<Map.Entry<String, Integer>> viewCountSet = viewCountMap.entrySet();
        viewCountSet.stream()
                .peek(set -> {
                    String key = set.getKey();
                    Integer viewCount = set.getValue();
                    articleService.updateArticleViewCount(Integer.valueOf(key),viewCount);
                }).collect(Collectors.toList());

       setRedisExpireTime(SystemConstance.REDIS_ARTICLE_VIEW_COUNT_KEY);

    }


    //定时 刷新 接口资源缓存键 过期时间
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void redisResourceScheduled(){
        setRedisExpireTime(SystemConstance.RESOURCE_REQUEST_URI_LIST_KEY);
    }

    //定时 刷新 网站配置缓存键 过期时间
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void redisWebsiteConfigScheduled(){
        setRedisExpireTime(SystemConstance.REDIS_WEBSITE_CONFIG_REVIEW_HASH_KEY);
    }



//    //定时刷新 文章评论 说说评论 友链评论 过期时间
//    @Scheduled(cron = "0 0 0/1 * * ?")
//    public void redisCommentByTypeScheduled(){
//
//        setRedisExpireTime(SystemConstance.REDIS_ARTICLE_COMMENT_CHILDREN_KEY);
//
//        setRedisExpireTime(SystemConstance.REDIS_TALK_COMMENT_CHILDREN_KEY);
//
//        setRedisExpireTime(SystemConstance.REDIS_FRIEND_LINK_COMMENT_CHILDREN_KEY);
//    }



    private void setRedisExpireTime(String hashKey){
        redisCache.setCacheObjectExpireTime(hashKey,
                SystemConstance.REDIS_CACHE_KEY_EXPIRE_TIME,
                SystemConstance.REDIS_TIMEUNIT);
    }




}
